import { EventTouch, instantiate, Prefab } from 'cc';
import { app } from '../../application';
import { HintTipInfo } from '../../declarations';
import { ccclass, ccmodifier } from '../../decorators';
import { HintTip } from './HintTip';
import { HintStack } from './HintStack';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.11.02
 * - 名称: HintTipStack
 * - 描述: 普通提示堆栈
 * - 说明: 普通提示允许同时存在多个，在展示指定时间后会自动消失
 */
@ccclass()
@ccmodifier('HintTipStack')
export class HintTipStack extends HintStack {
    /**
     * 添加提示
     * @param cfg 提示配置
     */
    async addTip(cfg: HintTipInfo) {
        const prefab = await app.resLoader.loadOne({ path: cfg.path, bundle: cfg.bundle, type: Prefab });
        if (prefab && prefab.data.getComponent(HintTip)) {
            const node = instantiate(prefab);
            const comp = node.getComponent(HintTip);
            this.node.addChild(node);
            await comp!.show(cfg);
        } else {
            this.logger.error('预制体不存在或未包含 HintTip 组件:', cfg, prefab);
        }
    }

    protected onTouchScreen(e: EventTouch) {
        e.preventSwallow = true;
    }

    async enter(): Promise<void> {
        await this.hide();
        this.logger.debug('进入舞台');
    }

    async exit() {
        this.node.destroyAllChildren();
        await super.exit();
    }
}
